package zelix.utils;

import java.lang.reflect.Field;

import zelix.utils.system.Mapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.network.Packet;
import net.minecraftforge.fml.relauncher.ReflectionHelper;

public class Wrapper {

    public static boolean SelfFontRender = true;

	public static volatile Wrapper INSTANCE = new Wrapper();

    public Minecraft mc() {
        return Minecraft.getMinecraft();
    }

    public EntityPlayerSP player() {
        return Wrapper.INSTANCE.mc().player;
    }

    public WorldClient world() {
        return Wrapper.INSTANCE.mc().world;
    }

    public GameSettings mcSettings() {
        return Wrapper.INSTANCE.mc().gameSettings;
    }

    public FontRenderer fontRenderer() {
        //return HUD.font.getFont("SFB 6");
        return Wrapper.INSTANCE.mc().fontRenderer;
    }
    
    public void sendPacket(Packet packet) {
        this.player().connection.sendPacket(packet);
    }
    
    public InventoryPlayer inventory() { 
		return this.player().inventory; 
	}
	
	public PlayerControllerMP controller() { 
		return Wrapper.INSTANCE.mc().playerController; 
	}

    public static double getRenderPosX() {
        return (Double)getField("renderPosX", "field_78725_b", Minecraft.getMinecraft().getRenderManager());
    }

    public static double getRenderPosY() {
        return (Double)getField("renderPosY", "field_78726_c", Minecraft.getMinecraft().getRenderManager());
    }

    public static double getRenderPosZ() {
        return (Double)getField("renderPosZ", "field_78723_d", Minecraft.getMinecraft().getRenderManager());
    }
    public static Object getField(final String field, final String obfName, final Object instance) {
        final Field fField = ReflectionHelper.findField((Class)instance.getClass(), Mapping.isNotObfuscated()?new String[] { field } : new String[] { obfName }

        );
        fField.setAccessible(true);
        try {
            return fField.get(instance);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
